var months = ['January', 'February', 'March', 'April', 'May' ]; //, 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

$(document).ready(function() {
	document.getElementById("finalsubmit").disabled = true; 
	$("#file1").change( function() {
	  $('#sales-volume td.ctr-val input').each( function(i, elem){
	  	var randomNr = Math.floor((Math.random() * 4000) /100) * 100 + 38000;
	  	$(elem).val(randomNr);
	  });
	});

	//uses underscore.js library for templating
	var compiled = _.template( $('#month-template').html() );
	
	//var months = ['January', 'February', 'March', 'April', 'May' ]; //, 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	
	
	for(var i=0; i < months.length; i++) {
		var randomNr = Math.floor((Math.random() * 4000) /100) * 100 + 38000;
		$('table#sales-volume tbody').append(compiled({month : months[i], forecast: 0}));
	}

	// tooltip
   	$('a[rel=tooltip]').popover({ html: true});
	
});

var validationComplete = false;

function submitOK(){
	return validationComplete;
}

function validate(){
	
	var rows = $("#inputform tbody tr").length;
	var correct = 0;
	$("#inputform tbody tr").each( function(i, elem){
		var row = elem;
		var valStr = $(elem).children(".ctr-val").children(":input").val().replace(',', '').replace('.', '');
		var val = parseFloat(valStr);
		var foreStr = $(elem).children('.ctr-forecast').text().replace(',', '').replace('.', '');
		var forecast = parseFloat(foreStr);
		var comment = $(elem).children('.ctr-comment').children('span');
		
		var lower = 0.8 * forecast;
		var upper = 1.2 * forecast;
		var cl =$(elem).attr('class'); 
		
		if(cl === 'warning'){
			
		}
		else if(val >= lower && val <= upper){
			correct += 1;
			$(comment).attr('class', 'label label-success');
			$(comment).text('Validation successful');
			$(elem).attr('class', 'success');
			$(elem).children('.ctr-comment').children('.ctr-justify').css('visibility', 'hidden');
			if($(elem).children('.ctr-comment').children('.ctr-justify').children('div').hasClass('in')){
				$(elem).children('.ctr-comment').children('.ctr-justify').children('div').collapse('hide');
			}
			
		}
		else{
			$(comment).attr('class', 'label label-important');
			$(comment).text('Validation failed - value differs greatly from forecast');
			$(elem).attr('class', 'error');
			var foo = $(elem).children('.ctr-comment').children('.ctr-justify');
			$(elem).children('.ctr-comment').children('.ctr-justify').css('visibility', 'visible');
		}
		
	});
	/*
	if(rows === correct){
		document.getElementById("finalsubmit").disabled = false; 
		$("#finalsubmit").attr('class', 'btn btn-primary' );
		validationComplete = true;
	}
	*/
	validateAll();
	
}


function validateAll(){
	var success = true;
	$("#inputform tbody tr").each( function(i, elem){
		var cl =$(elem).attr('class'); 
		if(cl !== 'warning' && cl !== 'success'){
			success = false;
		}
	});
	
	if(success){
		document.getElementById("finalsubmit").disabled = false; 
		$("#finalsubmit").attr('class', 'btn btn-primary' );
		validationComplete = true;
	}
}


function saveInputs(obj){
	var row = $(obj).parents('tr');
	var textarea = $(obj).siblings('textarea');
	var inp = $(textarea).val();
	var justify = $(row).children('.ctr-comment').children('.ctr-justify');
	var textcomment = $(justify).children('.collapse').children('.ctr-textcomment');
	var butsave = $(justify).children('.collapse').children('.butsave');
	var butedit = $(justify).children('.collapse').children('.butedit');
	if(inp.length > 5){
		$(row).attr('class', 'warning');
		var comment = $(row).children('.ctr-comment').children('span');
		$(comment).attr('class', 'label label-warning');
		$(comment).text('Reason for large deviation given.');
		$(textcomment).text(inp);
		$(textcomment).css('display', 'inline-block');
		$(textarea).css('display', 'none');
		$(butsave).css('display', 'none');
		$(butedit).css('display', 'inline-block');
		
		validateAll();
	}
	
}

function switchEdit(obj){
	var row = $(obj).parents('tr');
	var textarea = $(obj).siblings('textarea');
	var inp = $(textarea).val();
	var justify = $(row).children('.ctr-comment').children('.ctr-justify');
	var textcomment = $(justify).children('.collapse').children('.ctr-textcomment');
	var butsave = $(justify).children('.collapse').children('.butsave');
	var butedit = $(justify).children('.collapse').children('.butedit');
	if(inp.length > 5){
		$(textarea).text(inp);
		$(textarea).css('display', 'inline-block');
		$(textcomment).css('display', 'none');
		$(butsave).css('display', 'inline-block');
		$(butedit).css('display', 'none');
		
		validateAll();
	}
	
}

function resetRow(obj){
	var row = $(obj).parents('tr');
	var comment = $(row).children('.ctr-comment').children('span');
	
	$(row).removeClass('error');
	$(row).removeClass('warning');
	$(row).removeClass('success');
	$(comment).attr('class', 'label');
	$(comment).text('Validation required');
	
	$(row).children('.ctr-comment').children('.ctr-justify').css('visibility', 'hidden');
}

function resetAll() {
	var inputs = $('div#main-table input.input-small');
	$.each(inputs, function(index, input) {
		console.log(input.value);
		input.value="";
		resetRow(input);
	})
}

function finalSubmit() {
	$('div#messages').append("<p><em>You have successfully stored the following data:</em></p>");
	$('div#messages').append('<div id="storedData" class="storDat"></div>');
	$('div#storedData').append('<table class="table table-striped table-hover" id="finalTable"></table>');
	var inputs = $('div#main-table input.input-small');
	$.each(inputs, function(index, input) {
		var val = input.value;
		var mon = months[index];
		$('#finalTable').append('<tr><td>' + mon + ' </td><td> ' + val + '</td>');
	});
	//$('div#storedData').append('</table>');
	
	$('#main-table').remove();
	resetAll();
}

function openFileOption()
{
	document.getElementById("file1").click();
}